using System;
using Microsoft.Xna.Framework;
using SolarWinds.Effects;

namespace SolarWinds.Screens
{
    class BackgroundScreen : Screen
    {
        #region Constants

        /// <summary>
        /// The period of the parallax motion in the starfield.
        /// </summary>
        const float StarsParallaxPeriod = 30f;


        /// <summary>
        /// The amplitude of the parallax motion in the starfield.
        /// </summary>
        const float StarsParallaxAmplitude = 2048f;

        #endregion

        /// <summary>
        /// Persistent movement tracker, used to slightly parallax the stars.
        /// </summary>
        private double mMovement;

        private Starfield2D mStarField;

        public BackgroundScreen(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
        }

        public override void Initialize()
        {
            base.Initialize();

            mStarField = new Starfield2D(Game)
                             {
                                 Position = Vector2.Multiply(new Vector2(
                                                                 (float) Math.Cos(mMovement/StarsParallaxPeriod),
                                                                 (float) Math.Sin(mMovement/StarsParallaxPeriod)),
                                                             StarsParallaxAmplitude)
                             };

            Components.Add(mStarField);
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            // update the parallax movement
            mMovement += gameTime.ElapsedRealTime.TotalSeconds;
            Vector2 position = Vector2.Multiply(new Vector2(
                                                    (float)Math.Cos(mMovement / StarsParallaxPeriod),
                                                    (float)Math.Sin(mMovement / StarsParallaxPeriod)),
                                                StarsParallaxAmplitude);

            // update the stars
            mStarField.Position = position;
        }
    }
}